@echo off

if /i "%CONTOOLS_GITHUB_PROJECT_ROOT_INIT0_DIR%" == "%~dp0" exit /b 0

set "CONTOOLS_GITHUB_PROJECT_ROOT_INIT0_DIR=%~dp0"

rem cast to integer
set /A NEST_LVL+=0

rem Do not make a file or a directory
if defined NO_GEN set /A NO_GEN+=0

rem Do not make a log directory or a log file
if defined NO_LOG set /A NO_LOG+=0

rem Do not make a log output or stdio duplication into files
if defined NO_LOG_OUTPUT set /A NO_LOG_OUTPUT+=0

rem Do not change code page
if defined NO_CHCP set /A NO_CHCP+=0

call "%%~dp0canonical_path_if_ndef.bat" CONTOOLS_GITHUB_PROJECT_ROOT            "%%~dp0.."
call "%%~dp0canonical_path_if_ndef.bat" CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT  "%%CONTOOLS_GITHUB_PROJECT_ROOT%%/_externals"

if not exist "%CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT%\*" (
  echo.%~nx0: error: CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT directory does not exist: "%CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT%".
  exit /b 255
) >&2

call "%%~dp0canonical_path_if_ndef.bat" PROJECT_OUTPUT_ROOT                     "%%CONTOOLS_GITHUB_PROJECT_ROOT%%/_out"
call "%%~dp0canonical_path_if_ndef.bat" PROJECT_LOG_ROOT                        "%%CONTOOLS_GITHUB_PROJECT_ROOT%%/.log"

call "%%~dp0canonical_path_if_ndef.bat" CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT   "%%CONTOOLS_GITHUB_PROJECT_ROOT%%/_config"
call "%%~dp0canonical_path_if_ndef.bat" CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT  "%%PROJECT_OUTPUT_ROOT%%/config/contools--github"

rem retarget externals of an external project

call "%%~dp0canonical_path_if_ndef.bat" CONTOOLS_PROJECT_EXTERNALS_ROOT         "%%CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT%%"

rem init immediate external projects

if exist "%CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT%/contools/__init__/__init__.bat" (
  rem disable code page change in nested __init__
  set /A NO_CHCP+=1
  call "%%CONTOOLS_GITHUB_PROJECT_EXTERNALS_ROOT%%/contools/__init__/__init__.bat" -no_load_user_config || exit /b
  set /A NO_CHCP-=1
)

if %NO_GEN%0 EQU 0 (
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/mkdir_if_notexist.bat" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" || exit /b
)

if not defined LOAD_CONFIG_VERBOSE if %INIT_VERBOSE%0 NEQ 0 set LOAD_CONFIG_VERBOSE=1

if %NO_GEN%0 EQU 0 (
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/load_config_dir.bat" %%* -gen_user_config -- "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" || exit /b

  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" accounts-user.lst || exit /b
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" accounts-org.lst || exit /b

  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos.lst || exit /b
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos-auth.lst || exit /b

  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos-with-workflows.lst || exit /b
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos-auth-with-workflows.lst || exit /b

  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos-forked.lst || exit /b
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos-forked-parent.lst || exit /b

  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" repos-to-delete.lst || exit /b

  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" workflows.lst || exit /b
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/gen_config.bat" -if_notexist "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" workflows-inactive.lst || exit /b
) else call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/load_config_dir.bat" %%* -- "%%CONTOOLS_GITHUB_PROJECT_INPUT_CONFIG_ROOT%%" "%%CONTOOLS_GITHUB_PROJECT_OUTPUT_CONFIG_ROOT%%" || exit /b

call "%%CONTOOLS_ROOT%%/std/if_var_defined_and_file_exist.bat" CURL_EXECUTABLE || (
  echo.%~nx0: error: CURL_EXECUTABLE file path is not found: "%CURL_EXECUTABLE%"
  exit /b 255
) >&2

call "%%CONTOOLS_ROOT%%/std/if_var_defined_and_file_exist.bat" JQ_EXECUTABLE || (
  echo.%~nx0: error: JQ_EXECUTABLE file path is not found: "%JQ_EXECUTABLE%"
  exit /b 255
) >&2

call "%%CONTOOLS_ROOT%%/std/canonical_path.bat" GH_BACKUP_DIR   "%%GH_BACKUP_DIR%%"
call "%%CONTOOLS_ROOT%%/std/canonical_path.bat" GH_WORKFLOW_DIR "%%GH_WORKFLOW_DIR%%"

call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/mkdir_if_notexist.bat" "%%GH_BACKUP_DIR%%" || exit /b
call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/mkdir_if_notexist.bat" "%%GH_WORKFLOW_DIR%%" || exit /b

if %NO_GEN%0 EQU 0 (
  call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/mkdir_if_notexist.bat" "%%PROJECT_OUTPUT_ROOT%%" || exit /b
)

if %NO_CHCP%0 EQU 0 (
  if defined CHCP call "%%CONTOOLS_ROOT%%/std/chcp.bat" %%CHCP%%
)

exit /b 0
